Function Minimization


Problem 1
Create a Wintempla dialog application called OneDimension to minimize the function shown.
Cree una aplicación de diálogo de Wintempla llamada OneDimension para minimizar la función mostrada.

OneDimension

OneDimensionRun

OneDimension.h
...
class OneDimension: public Win::Dialog, public Math::IFunction
{
public:
     OneDimension()
     {
     }
     ~OneDimension()
     {
     }
     //____________________________ Math::IFunction
     double EvaluateFunc(const double x);
     ...
};

OneDimension.cpp
...
void OneDimension::Window_Open(Win::Event& e)
{
     Math::Point a, b, c;
     a.x = 0;
     b.x = 1;
     Math::FindMinimum::Bracketing(*this, a, b, c);
     double minX;
     double fx = Math::FindMinimum::Brent(*this, a.x, b.x, c.x, 1.0e-10, minX);
     wstring text;
     Sys::Format(text, L"x = %g\r\n f(x) = %g", minX, fx);
     this->MessageBox(text, L"Solution", MB_OK);
}

double OneDimension::EvaluateFunc(const double x)
{
     return 3*x*x+5*x+6;
}


Problem 2
Create a Wintempla dialog application called Multidimension to minimize the function shown using only function evaluations. Use Wintempla to check the events: Timer and App.
Cree una aplicación de diálogo de Wintempla llamada Multidimension para minimizar la función mostrada usando solamente evaluación de funciones. Use Wintempla para seleccionar los eventos: Timer y App.

MultiEvent1

MultiEvent2

MultidimensionFunction

MultidimensionRun

Multidimension.h
#pragma once //______________________________________ Multidimension.h
#include "Resource.h"
#define MAIN_TIMER 1
#define WORK_ID 1
class Multidimension: public Win::Dialog, public Math::IMultiVarFunc
{
public:
     Multidimension()
     {
     }
     ~Multidimension()
     {
     }
     Mt::DoubleTs error;
     Math::PowellFindMinMV findmin;
     Mt::ThreadObject threadObject;
     //________________________________________________Math::IMultiVarFunc
     double EvaluateFunc(const valarray<double>& x);
protected:
     ...
};


Multidimension.cpp
...
void Multidimension::Window_Open(Win::Event& e)
{
     valarray<double> startingPoint;
     startingPoint.resize(2);
     startingPoint[0] = 0.0;
     startingPoint[1] = 0.0;
     findmin.Setup(error, *this, startingPoint, 100);
     findmin.SetPostMessage(hWnd, WM_APP, 0, WORK_ID);
     threadObject.BeginThread(findmin);
     timer.Set(MAIN_TIMER, 1000);
}

double Multidimension::EvaluateFunc(const valarray<double>& x)
{
     const double X = x[0];
     const double Y = x[1];
     const double a = X - M_PI;
     const double b = Y - M_PI;
     return -cos(X)*cos(Y)*exp(-(a*a + b*b));
}

void Multidimension::Window_Timer(Win::Event& e)
{
     wchar_t text[256];
     threadObject.GetProgressInfo(text, 256);
     this->Text = text;
}

void Multidimension::Window_App(Win::Event& e)
{
     if (e.lParam == WORK_ID)
     {
          this->timer.Kill(MAIN_TIMER);
          threadObject.WaitForExit();
          this->Text = L"Done!";
          //________________ Display result
          wstring result;
          Sys::Format(result, L"X = %g\r\nY = %g\r\n F(X, Y) = %g", findmin.solution[0], findmin.solution[1], error.Get());
          this->MessageBox(result, L"Solution", MB_OK);
     }
}


Problem 3
Create a Wintempla dialog application called MulDerivative to minimize the function shown using the gradient of the function. Use Wintempla to check the events: Timer and App.
Cree una aplicación de diálogo de Wintempla llamada MulDerivative para minimizar la función mostrada usando el gradiente de la función. Use Wintempla para seleccionar los eventos: Timer y App.

DerEvent1

DerEvent2

MulDerivative

MulDerivativeRun

MulDerivative.h
#pragma once //______________________________________ MulDerivative.h
#include "Resource.h"
#define MAIN_TIMER 1
#define WORK_ID 1
class MulDerivative: public Win::Dialog, public Math::IMultiVarFuncD
{
public:
     MulDerivative()
     {
     }
     ~MulDerivative()
     {
     }
     Mt::DoubleTs error;
     Math::FindMinimumMV findmin;
     Mt::ThreadObject threadObject;
     //________________________________________________Math::IMultiVarFuncD
     double EvaluateFunc(const valarray<double>& x);
     void EvaluateFuncAndGrad(const valarray<double>& x, double& Fx, valarray<double>& gradient);
protected:
     ...
};


MulDerivative.cpp
...
void MulDerivative::Window_Open(Win::Event& e)
{
     valarray<double> startingPoint;
     startingPoint.resize(2);
     startingPoint[0] = 0.0;
     startingPoint[1] = 0.0;
     findmin.Setup(error, *this, startingPoint, 100, 1.0e-10, WT_MATH_VARMETRIC);
     findmin.SetPostMessage(hWnd, WM_APP, 0, WORK_ID);
     threadObject.BeginThread(findmin);
     timer.Set(MAIN_TIMER, 1000);
}

double MulDerivative::EvaluateFunc(const valarray<double>& x)
{
     const double X = x[0];
     const double Y = x[1];
     //_______________________________________________ F(X, Y)
     const double a = (X +2*Y-7);
     const double b = (2*X +Y-5);
     return a*a + b*b;
}

void MulDerivative::EvaluateFuncAndGrad(const valarray<double>& x, double& Fx, valarray<double>& gradient)
{
     //_______________________________________________ F(X, Y)
     Fx = EvaluateFunc(x);
     //_______________________________________________ gradient
     const double X = x[0];
     const double Y = x[1];
     gradient[0] = 10*X+8*Y-34; // d F(X, Y) / dx
     gradient[1] = 8*X+10*Y-38; // d F(X, Y) / dy
}

void MulDerivative::Window_Timer(Win::Event& e)
{
     if (threadObject.IsStillActive())
     {
          wchar_t text[256];
          threadObject.GetProgressInfo(text, 256);
          this->Text = text;
     }
}

void MulDerivative::Window_App(Win::Event& e)
{
     if (e.lParam == WORK_ID)
     {
          this->timer.Kill(MAIN_TIMER);
          threadObject.WaitForExit();
          this->Text = L"Done!";
          //________________ Display result
          wstring result;
          Sys::Format(result, L"X = %g\r\nY = %g\r\n F(X, Y) = %g", findmin.solution[0], findmin.solution[1], error.Get());
          this->MessageBox(result, L"Solution", MB_OK);
     }
}

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home